<%@ page contentType="text/html;charset=UTF-8" %>
<script>
$(document).ready(function() {
	$('#storeBudgetTable').bootstrapTable({
		 
		  //请求方法
               method: 'get',
        height:600,
        showFooter: true,//必须
        onPostBody:function () {
            //合并页脚
            merge_footer();
        },
               //类型json
               dataType: "json",
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
               //显示 内容列下拉框
    	       showColumns: true,
    	       //显示到处按钮
    	       showExport: true,
    	       //显示切换分页按钮
    	       showPaginationSwitch: true,
    	       //显示详情按钮
    	       // detailView: true,
    	       // 	//显示详细内容函数
	           // detailFormatter: "detailFormatter",
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）     
               cache: false,    
               //是否显示分页（*）  
               pagination: true,   
                //排序方式 
               sortOrder: "asc",  
               //初始化加载第一页，默认第一页
               pageNumber:1,   
               //每页的记录行数（*）   
               pageSize: 10,  
               //可供选择的每页的行数（*）    
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据  
               url: "${ctx}/infor/storeBudget/data1",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',   
               ////查询参数,每次调用是会带上这个参数，可自定义                         
               queryParams : function(params) {
               	var searchParam = $("#searchForm").serializeJSON();
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                   	edit(row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该仓租收支明细记录吗？', function(){
                       	jp.loading();
                       	jp.get("${ctx}/infor/storeBudget/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#storeBudgetTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})
                   	   
                   	});
                      
                   } 
               },
              
               onClickRow: function(row, $el){
               },
               columns: [{
		        field: 'pro',
		        title: '项目',
		        sortable: true,
                   footerFormatter: function (value) {

                       var count = 0.0;
                       var co=0.0;
                       var kp=0.0;
                       var mlr=0.0;
                       for (var i=0; i<value.length;i++) {
                           count += parseFloat(value[i].zsr);
                       }

                       var lrl=mlr/count;

                       return "筛选合计：  总收入："+ count.toFixed(2);
                   }

		       
		    }  ,{
                   field: 'bsc',
                   title: '办事处',
                   sortable: true

               }
			,{
		        field: 'djh',
		        title: '单据号',
		        sortable: true

		    }
			,{
		        field: 'rq',
		        title: '日期',
		        sortable: true
		       
		    }


			,{
		        field: 'sl',
		        title: '税率',
		        sortable: true
		       
		    }
			,{
		        field: 'mj',
		        title: '仓库面积',
		        sortable: true
		       
		    }
			,{
		        field: 'srdj',
		        title: '收入单价',
		        sortable: true
		       
		    }
			,{
		        field: 'srfj',
		        title: '收入附加',
		        sortable: true
		       
		    }
			,{
		        field: 'sr',
		        title: '不含税收入',
		        sortable: true
		       
		    }
			,{
		        field: 'hssr',
		        title: '含税收入',
		        sortable: true
		       
		    }
			,{
		        field: 'glsr',
		        title: '仓库管理收入',
		        sortable: true
		       
		    }
			,{
		        field: 'zsr',
		        title: '总收入',
		        sortable: true
		       
		    },{
                       field: 'kpje',
                       title: '开票金额',
                       sortable: true

                   },{
                       field: 'kprq',
                       title: '开票日期',
                       sortable: true

                   },{
                       field: 'fph',
                       title: '发票号',
                       sortable: true

                   },{
                       field: 'dzje',
                       title: '到账金额',
                       sortable: true

                   }

		     ]
		
		});
		
		  
	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端

		 
		  $('#storeBudgetTable').bootstrapTable("toggleView");
		}
	  
	  $('#storeBudgetTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', ! $('#storeBudgetTable').bootstrapTable('getSelections').length);
          $('#kp').prop('disabled', ! $('#storeBudgetTable').bootstrapTable('getSelections').length);
            $('#edit').prop('disabled', $('#storeBudgetTable').bootstrapTable('getSelections').length!=1);
          $('#qr').prop('disabled', $('#storeBudgetTable').bootstrapTable('getSelections').length!=1);
          $('#fk').prop('disabled', $('#storeBudgetTable').bootstrapTable('getSelections').length!=1);

          $('#qxqr').prop('disabled', $('#storeBudgetTable').bootstrapTable('getSelections').length!=1);

      });
		  
		$("#btnImport").click(function(){
			jp.open({
			    type: 1, 
			    area: [500, 300],
			    title:"导入数据",
			    content:$("#importBox").html() ,
			    btn: ['下载模板','确定', '关闭'],
				    btn1: function(index, layero){
					  window.location='${ctx}/infor/storeBudget/import/template';
				  },
			    btn2: function(index, layero){
				        var inputForm =top.$("#importForm");
				        var top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe 
				        inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
				        inputForm.onsubmit = function(){
				        	jp.loading('  正在导入，请稍等...');
				        }
				        inputForm.submit();
					    jp.close(index);
				  },
				 
				  btn3: function(index){ 
					  jp.close(index);
	    	       }
			}); 
		});
		    
	  $("#search").click("click", function() {// 绑定查询按扭
		  $('#storeBudgetTable').bootstrapTable('refresh');
		});
	 
	 $("#reset").click("click", function() {// 绑定查询按扭
		  $("#searchForm  input").val("");
		  $("#searchForm  select").val("");
		   $("#searchForm  .select-item").html("");
		  $('#storeBudgetTable').bootstrapTable('refresh');
		});
		
		
	});
		
  function getIdSelections() {
        return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }
function getstatus() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.djzt
    });
}
function merge_footer() {
    var footer_tbody = $('.fixed-table-footer table tbody');
    var footer_tr = footer_tbody.find('>tr');
    var footer_td = footer_tr.find('>td');
    var footer_td_1 = footer_td.eq(0);
    //第一列为:'平均分',第二列为平局分内容
    for(var i=1;i<footer_td.length;i++) {
        footer_td.eq(i).hide();
    }
    footer_td_1.attr('colspan', 3).show();
}
function getkp() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.kpzt
    });
}
function getjs() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.jszt
    });
}
function getsr() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.zsr
    });
}
function getzc() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.zzc
    });
}
function getyf() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.yf
    });
}
function getwf() {
    return $.map($("#storeBudgetTable").bootstrapTable('getSelections'), function (row) {
        return row.wf
    });
}
function fk() {

    if(getstatus()=="已创建"){
        return;
    }

    if(parseFloat(getwf())==0){
        return;
    }
    $("#yfzje").val(getzc());
    $("#yfje").val(getyf());
    $("#wfje").val(getwf());
    $("#fkje").val(getwf());
    $("#myModal1").modal("show");
}
function fkqd() {
    var wf=getwf();
    var fk=$("#fkje").val();
    if(parseFloat(fk)>parseFloat(wf)){
        jp.warning("付款金额大于未付金额！")
        return;
    }
    $.ajax({
        url:'${ctx}/infor/storeBudget/fk?id='+getIdSelections()+"&je="+fk,
        success:function (data) {
            $("#myModal1").modal("hide");
            $('#storeBudgetTable').bootstrapTable('refresh');
            jp.success("付款成功");

        }
    })

}
function kp() {
    var status=getstatus();
    var kp=getkp();
    var kps=kp.toString().split(",");
    var s=status.toString().split(",");
    var bol=false;
    for(var i=0;i<s.length;i++){
        if(s[i]!="已确认"){
            bol=true;
        }
    }
    for(var i=0;i<kps.length;i++){
        if(kps[i]!="未开票"){
            bol=true;
        }
    }
    if(bol){
        return;
    }

    var j=getsr();

    var jes=j.toString().split(",");
    var zje=0.0;
    for(var i=0;i<jes.length;i++){
        zje+=parseFloat(jes[i].toString().replace(/,/,""));
    }
    jp.openDialog('开票', "${ctx}/infor/rentJs/form1?je="+zje+"&i="+getIdSelections(),'1000px', '800px', $('#storeBudgetTable'));

}
function qr() {
    if(getstatus()!="已创建"){
        return;
    }
    jp.confirm('确定要确认该记录吗？', function(){
        jp.loading();
        jp.get("${ctx}/infor/storeBudget/qr?id=" + getIdSelections(), function(data){
            if(data.success){
                $('#storeBudgetTable').bootstrapTable('refresh');
                jp.success(data.msg);
            }else{
                jp.error(data.msg);
            }
        })

    })
}
function qxqr() {
    if(getstatus()!="已确认"||getkp()=="已开票"||getjs()!="未结算"){
        return;
    }
    jp.confirm('确定要取消确认该记录吗？', function(){
        jp.loading();
        jp.get("${ctx}/infor/storeBudget/qxqr?id=" + getIdSelections(), function(data){
            if(data.success){
                $('#storeBudgetTable').bootstrapTable('refresh');
                jp.success(data.msg);
            }else{
                jp.error(data.msg);
            }
        })

    })
}
  function deleteAll(){

		jp.confirm('确认要删除该仓租收支明细记录吗？', function(){
			jp.loading();  	
			jp.get("${ctx}/infor/storeBudget/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#storeBudgetTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})
          	   
		})
  }
  
  function add(){
	  jp.openDialog('新增仓租收支明细', "${ctx}/infor/storeBudget/form",'1400px', '1200px', $('#storeBudgetTable'));
  }
  function view(id) {
      jp.openDialogView('查看仓租收支明细', "${ctx}/infor/storeBudget/form?id=" + id,'1600px', '1200px', $('#storeBudgetTable'));

  }
  function edit(){//没有权限时，不显示确定按钮
      if(getstatus()!="已创建"){
          return;
      }
	  jp.openDialog('编辑仓租收支明细', "${ctx}/infor/storeBudget/form?id=" + getIdSelections(),'1600px', '1200px', $('#storeBudgetTable'));

  }
  
  
  
  
		   
  function detailFormatter(index, row) {
	  var htmltpl =  $("#storeBudgetChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
	  var html = Mustache.render(htmltpl, {
			idx:row.id
		});
	  $.get("${ctx}/infor/storeBudget/detail?id="+row.id, function(storeBudget){
    	var storeBudgetChild1RowIdx = 0, storeBudgetChild1Tpl = $("#storeBudgetChild1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data1 =  storeBudget.budgetExpenList;
		for (var i=0; i<data1.length; i++){
			addRow('#storeBudgetChild-'+row.id+'-1-List', storeBudgetChild1RowIdx, storeBudgetChild1Tpl, data1[i]);
			storeBudgetChild1RowIdx = storeBudgetChild1RowIdx + 1;
		}
				
    	var storeBudgetChild2RowIdx = 0, storeBudgetChild2Tpl = $("#storeBudgetChild2Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data2 =  storeBudget.budgetIncomeList;
		for (var i=0; i<data2.length; i++){
			addRow('#storeBudgetChild-'+row.id+'-2-List', storeBudgetChild2RowIdx, storeBudgetChild2Tpl, data2[i]);
			storeBudgetChild2RowIdx = storeBudgetChild2RowIdx + 1;
		}
				
      	  			
      })
     
        return html;
    }
  
	function addRow(list, idx, tpl, row){
		$(list).append(Mustache.render(tpl, {
			idx: idx, delBtn: true, row: row
		}));
	}
			
</script>
<script type="text/template" id="storeBudgetChildrenTpl">//<!--
	<div class="tabs-container">
		<ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-{{idx}}-1" aria-expanded="true">仓租管理支出</a></li>
				<li><a data-toggle="tab" href="#tab-{{idx}}-2" aria-expanded="true">仓租管理收入</a></li>
		</ul>
		<div class="tab-content">
				 <div id="tab-{{idx}}-1" class="tab-pane fade in active">
						<table class="ani table">
						<thead>
							<tr>
								<th>支出类型</th>
								<th>支出金额</th>
								<th>备注信息</th>

							</tr>
						</thead>
						<tbody id="storeBudgetChild-{{idx}}-1-List">
						</tbody>
					</table>
				</div>
				<div id="tab-{{idx}}-2" class="tab-pane fade">
					<table class="ani table">
						<thead>
							<tr>
								<th>收入类型</th>
								<th>收入金额</th>
								<th>收入说明</th>

							</tr>
						</thead>
						<tbody id="storeBudgetChild-{{idx}}-2-List">
						</tbody>
					</table>
				</div>
		</div>//-->
	</script>
	<script type="text/template" id="storeBudgetChild1Tpl">//<!--
				<tr>
					<td>
						{{row.type}}
					</td>
					<td>
						{{row.je}}
					</td>
					<td>
						{{row.remarks}}
					</td>

				</tr>//-->
	</script>
	<script type="text/template" id="storeBudgetChild2Tpl">//<!--
				<tr>
					<td>
						{{row.type}}
					</td>
					<td>
						{{row.je}}
					</td>
					<td>
						{{row.remarks}}
					</td>

				</tr>//-->
	</script>
